#pragma warning disable CS8765 // Nullability of type of parameter doesn't match overridden member (possibly because of nullability attributes).

namespace Crux.Core.Dapper.Internals;
internal class WrappedConnection
    : DbConnection
{
    private readonly DbConnection _connection;
    private readonly IExecuteListener _executeListener;
    public WrappedConnection(
        DbConnection connection,
        IExecuteListener executeListener)
    {
        _connection = connection;
        _executeListener = executeListener;
    }
    public override string ConnectionString
    {
        get => _connection.ConnectionString;
        set => _connection.ConnectionString = value;
    }
    public override string Database => _connection.Database;
    public override string DataSource => _connection.DataSource;
    public override string ServerVersion => _connection.ServerVersion;
    public override ConnectionState State => _connection.State;
    protected override void Dispose(bool disposing)
    {
        if (disposing)
            _connection?.Dispose();

        base.Dispose(disposing);
    }
    public override void ChangeDatabase(string databaseName)
    {
        _connection.ChangeDatabase(databaseName);
    }
    public override void Close()
    {
        var stopwatch = Stopwatch.StartNew();

        _connection.Close();

        _executeListener.ConnectionClosed(_connection, stopwatch.ElapsedMilliseconds);
    }

    public override void Open()
    {
        var stopwatch = Stopwatch.StartNew();
        _connection.Open();

        _executeListener.ConnectionOpened(_connection, stopwatch.ElapsedMilliseconds);
    }

    protected override DbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
    {
        return _connection.BeginTransaction(isolationLevel);
    }

    protected override DbCommand CreateDbCommand()
    {
        return new WrappedCommand(_connection.CreateCommand(), _executeListener);
    }
}
#pragma warning restore CS8765 // Nullability of type of parameter doesn't match overridden member (possibly because of nullability attributes).
